package com.ffcs.ffxtc.modules.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ffcs.ffxtc.modules.base.dao.DrawdbUserService;
import com.ffcs.ffxtc.modules.base.domin.entity.DrawdbUser;
import com.ffcs.ffxtc.modules.base.service.UserService;
import com.ffcs.utils.JWTUtils;
import com.ffcs.utils.MD5Utils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author zhangzhengxing
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final DrawdbUserService drawdbUserService;

    @Override
    public String login(DrawdbUser drawdbUser) {
        DrawdbUser user = drawdbUserService.getOne(new QueryWrapper<DrawdbUser>()
                .eq(DrawdbUser.USER_NAME, drawdbUser.getUserName())
                .eq(DrawdbUser.PASSWORD, MD5Utils.getMD5(drawdbUser.getPassword()))
        );
        if (ObjectUtil.isEmpty(user)) {
            throw new RuntimeException("用户或密码错误!");
        }

        HashMap<String, Object> userInfo = new HashMap<>(10);
        userInfo.put("userName", user.getUserName());
        userInfo.put("id", String.valueOf(user.getId()));
        userInfo.put("realname",user.getRealname());

        return JWTUtils.createJwt(userInfo);
    }
}